/**
 *  Copyright 2015 WalkTheTalkTech, Inc. All rights reserved.
 */
package walkthetalktech.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * 工具类
 *
 * @author mengfanxin
 *
 */
public class StringUtils {
	
	protected static Logger logger = LoggerFactory.getLogger(StringUtils.class);
	private static SimpleDateFormat f = new SimpleDateFormat("yyyyMMddHHmmss");
	private static SimpleDateFormat f3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	private static SimpleDateFormat f2 = new SimpleDateFormat("yyyyMMdd");

	/**
	 * 判断是否为数字
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str){
	    Pattern pattern = Pattern.compile("[0-9]*");
	    return pattern.matcher(str).matches();     
	}
	
	/**
	 * 是否为空
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmptyString(Object str){
	    if (str== null) {
	    	return true;
	    }
	    if (str instanceof String) {
	    	if (((String)str).length() <= 0) {
	    		return true;
	    	}
	    } else if (str instanceof Integer || str instanceof Long) {
	    		return false;
	    } else {
	    	logger.warn("parameter is not String." + str.getClass());
	    	return true;
	    }
	    
	    return false;
	}
	
	public static String getSuffixByUserType(int userType) {
		if (userType == 0) {
			return "STU";
		} else if (userType == 1) {
			return "TCR";
		} else {
			return "SRV";
		}
	}
	
	public static Date parseStringDateStart(String date) {
    	try {
			return f.parse(date + "000000");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			logger.error("parse Date error. input=" + date, e);
			return null;
		}
	}
	
	public static Date parseStringDateEnd(String date) {
		
    	try {
			return f.parse(date + "235959");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			logger.error("parse Date error. input=" + date, e);
			return null;
		}
	}
	
	public static String parseDate(Date date) {
		if(date != null) {
			return f2.format(date);
		} else {
			return null;
		}
	}
	
	public static String parseDetailDate(Date date) {
		if(date != null) {
			return f3.format(date);
		} else {
			return "";
		}
	}
	
}
